package com.darthsith.droidathome.core.rest.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.darthsith.droidathome.core.model.Device;
import com.darthsith.droidathome.core.model.User;
import com.darthsith.droidathome.core.service.AuthService;

@Controller
public class AuthController {
	
	private static final Log logger=LogFactory.getLog(AuthController.class);
	
	@Autowired
	private AuthService authService;
	
	@RequestMapping(value="/addUser&username={username}&firstname={firstname}&lastname={lastname}&email={email}")
	public void addUser(@PathVariable String username, @PathVariable String firstname, @PathVariable String lastname, @PathVariable String email) {
		User user=new User(username, firstname, lastname, email);
		authService.addUser(user);
		logger.info("added user: "+user);
	}
	
	@RequestMapping(value="/addUser", method=RequestMethod.POST)
	public void addUser(@ModelAttribute User user, BindingResult result) {
		authService.addUser(user);
	}
	
	@RequestMapping(value="/addDevice&mac={macAddress}&name={name}")
	public void addDevice(@PathVariable String macAddress, @PathVariable String name) {
		Device device=new Device(macAddress, name);
		authService.addDevice(device);
		logger.info("added device: "+device);
	}
	
	@RequestMapping(value="/addDevice", method=RequestMethod.POST)
	public @ResponseBody boolean addDevice(@RequestBody Device device) {
		authService.addDevice(device);
		logger.info("Device "+device);
		return true;
	}
	
}
